﻿using MathNet.Numerics;
using SeeSharpTools.JY.DSP.Utility.Filter1D;
using Sunny.UI;
using System;
using System.Data;
using System.Linq;

namespace SeeSharpTools.Demo
{
    public partial class FormResample : UIPage
    {
        public FormResample()
        {
            InitializeComponent();
        }

        private void uiButton_Start_Click(object sender, EventArgs e)
        {
            double originalFs = uiDoubleUpDown_OriginalFs.Value;
            double desiredFs = uiDoubleUpDown_DesiredFs.Value;

            // 有理分数近似
            var (p, q) = Multirate.RationalFraction(desiredFs / originalFs);
            uiTextBox_UpFactor.Text = p.ToString();
            uiTextBox_DownFactor.Text = q.ToString();

            // 生成频率为500 Hz的正弦信号，并按照新的采样率进行重采样
            double[] tx = Generate.LinearRange(0, 1 / originalFs, 0.01);
            double[] x = tx.Select(num => Math.Sin(2 * Math.PI * 500 * num)).ToArray();

            var y = Multirate.Resample(x, p, q);
            double[] ty = Generate.LinearRange(0, 1 / desiredFs, (y.Length - 1) / desiredFs);

            double[][] xData = new double[2][];
            double[][] yData = new double[2][];
            xData[0] = tx;
            xData[1] = ty;
            yData[0] = x;
            yData[1] = y;
            easyChartX1.Plot(xData, yData);
            easyChartX1.Series[0].Name = "原始信号";
            easyChartX1.Series[1].Name = "重采样信号";
            easyChartX1.Series[1].Type = SeeSharpTools.JY.GUI.EasyChartXSeries.LineType.Point;
        }
    }
}